<template>
  <div>
    <boxs></boxs>
    <van-pull-refresh v-model="isRefresh" @refresh="onRefresh">
      <van-list v-model="isloading" @load="onLoad" :finished="isover" finished-text="没有更多了">
        <van-card
          v-for="(item,index) in goodslist"
          :key="index"
          :num="item.goods_number"
          :price="item.goods_price"
          :title="item.goods_name"
          :thumb=" item.goods_small_logo"
          @click="goGoodsDetail(item.goods_id)"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
export default {
  data() {
    return {
      query: "",
      cid: "",
      pagenum: 1,
      pagesize: 10,
      goodslist: [],
      total: 0,
      isover: false,
      isloading: false,
      isRefresh: false
    };
  },
  created() {
    this.query = this.$route.query.query || "";
    this.cid = this.$route.query.cid || "";
  },
  mounted() {
    // this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      this.isloading = true;
      const { data: res } = await this.$http.get("/goods/search", {
        params: {
          query: this.query,
          cid: this.cid,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      });
      this.isRefresh = false;
      if (res.meta.status !== 200) {
        return this.$toast("数据获取失败");
      }
      if (res.message.total == 0) {
        this.isover = true;
      }
      this.goodslist = [...this.goodslist, ...res.message.goods];
      this.total = res.message.total;
      this.isloading = false;
    },
    onRefresh() {
      this.pagenum = 1;
      this.total = 0;
      this.goodslist = [];
      this.isover = this.isloading = false;
      this.onLoad();
    },
    onLoad() {
      if (this.isover) {
        return;
      }
      if (this.total == 0) {
        this.getGoodsList();
        return;
      }
      if (this.goodslist.length >= this.total) {
        this.isover = true;
        return;
      }
      this.pagenum++;
      this.getGoodsList();
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    goGoodsDetail(goods_id) {
      this.$router.push("/pages/goods_detail/" + goods_id);
    }
  }
};
</script>

<style lang="less" scoped>
</style>